(function($) {

	app = $.sammy('#content', function() {

		var that = this;
		var questionnairesView = new $.fn.QuestionnairesView();
		var questionnaireApp = new $.fn.QuestionnaireApp();

		// routes
		//***************************************************
		this.get('#/', function(context) {
			getQuestionnaireList(context);
		});

		this.get('#/questionnaire/new', function(context) {
			$.ajax({
				url:"questionnaires.php?method=getNewQuestionnaire",
				dataType: 'json',
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus == "action_unsuccessfull") {
						$('.appStatus').text("Laden fehlgeschlagen: " + oResponse.sReason)
						.removeClass("ui-state-highlight")
						.addClass("ui-state-error");
					} else {
						questionnaireApp.showQuestionnaire(oResponse.oQuestionnaire);
					}
				}
			});
		});

		this.get('#/questionnaire/:id', function(context) {
			$.ajax({
				url:'questionnaires.php?method=getInactiveQuestionnaire&id=' + this.params['id'],
				dataType: 'json',
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus == "action_unsuccessfull") {
						$('.appStatus').text("Laden fehlgeschlagen: " + oResponse.sReason)
						.removeClass("ui-state-highlight")
						.addClass("ui-state-error");
					} else {
						questionnaireApp.showQuestionnaire(oResponse.oQuestionnaire);
					}
				}
			});
		});

		this.post('#/questionnaire/new', function(context) {
			//alert(questionnaireApp.getQuestionnaire().title);
			$.ajax({
				url:'questionnaires.php?method=saveNew',
				dataType: 'json',
				data: {oQuestionnaire : questionnaireApp.getQuestionnaire()},
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus == "action_successfull") {
						$('.appStatus').text("Gespeichert!")
						.removeClass("ui-state-error")
						.addClass("ui-state-highlight");
					} else {
						$('.appStatus').text("Speichern fehlgeschlagen: " + oResponse.sReason)
						.removeClass("ui-state-highlight")
						.addClass("ui-state-error");
					}
				}
			});
		});

		this.post('#/questionnaire/:id', function(context) {
			$.ajax({
				url:'questionnaires.php?method=save',
				dataType: 'json',
				data: this.params,
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus == "action_successfull") {
						$('.appStatus').text("Gespeichert!")
						.removeClass("ui-state-error")
						.addClass("ui-state-highlight");
					} else {
						$('.appStatus').text("Speichern fehlgeschlagen: " + oResponse.sReason)
						.removeClass("ui-state-highlight")
						.addClass("ui-state-error");
					}
				}
			});
		});

		/**
		 * 
		 */
		getQuestionnaireList = function(context) {
			$.ajax({
				url:'questionnaires.php?method=getInactiveQuestionnaires',
				dataType: 'json',
				async: false,
				success: function(oResponse) {
					questionnairesView.showQuestionnaireList(oResponse, context); // view init
				}
			});
		};
	});

	$(function() {
		app.run('#/');
	});

})(jQuery);
